<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
        <title>Blank App</title>
        <style type="text/css">
        	body {
        		padding-top: 50px;
        	}
        	button {
        		width: 80%;
        		margin-left: 10%;
        		height: 80px;
        		background-color: skyblue;
        	}
        </style>
    </head>
    <body>
    <button type="button" id="btn1">取得传感器现在的值</button>
    <button type="button" id="btn2">开始监视</button>
    <button type="button" id="btn3">停止监视</button>
    <div id="output"></div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript">
document.addEventListener("deviceready", function(){
	alert("init");
	document.getElementById("btn1").addEventListener("touchend", function(){
		alert("getCurrentAcceleration");
		function onSuccess(acceleration) {
			alert("Success");
			document.getElementById("output").innerText = 
			'Acceleration X: ' + acceleration.x + '\n' +
			'Acceleration Y: ' + acceleration.y + '\n' +
			'Acceleration Z: ' + acceleration.z + '\n' +
			'Timestamp: '      + acceleration.timestamp + '\n';
		}
			
		function onError() {
			alert('onError!');
		}
			
		navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
	});


	document.getElementById("btn2").addEventListener("touchend", function(){
		function onSuccess(acceleration) {
			document.getElementById("output").innerText = 
			'Acceleration X: ' + acceleration.x + '\n' +
			'Acceleration Y: ' + acceleration.y + '\n' +
			'Acceleration Z: ' + acceleration.z + '\n' +
			'Timestamp: '      + acceleration.timestamp + '\n';
		}

		function onError() {
		    alert('onError!');
		}

		var options = { frequency: 100 };

		watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
	});

	document.getElementById("btn3").addEventListener("touchend", function(){
		navigator.accelerometer.clearWatch(watchID);
	});
})
</script>
    </body>
</html>
